package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.mapper.SgmOtherVulnerabilityMapper;
import com.yl.applicationsecurity.mapper.SgmSafeRiskItemMapper;
import com.yl.applicationsecurity.pojo.ScriptState;
import com.yl.applicationsecurity.pojo.SgmOtherVulnerability;
import com.yl.applicationsecurity.pojo.SgmSafeRisk;
import com.yl.applicationsecurity.pojo.SgmSafeRiskItem;
import com.yl.applicationsecurity.service.SgmOtherVulnerabilityService;
import com.yl.applicationsecurity.service.SgmSafeRiskItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-03-06 14:55:12
 */
@Service
public class SgmOtherVulnerabilityServiceImpl  extends ServiceImpl<SgmOtherVulnerabilityMapper, SgmOtherVulnerability> implements SgmOtherVulnerabilityService {

    @Autowired
    private ScriptStateMapper scriptStateMapper;

    @Override
    public PageInfo<SgmOtherVulnerability> findListBySgmOtherVulnerability(String[] nationSelect, String[] systemNameSelect, SgmOtherVulnerability sgmOtherVulnerability, Date startDate, Date endDate, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<SgmOtherVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();


        //根据URL模糊查询
        if ( StringUtils.isNotEmpty(sgmOtherVulnerability.getUrl())){
            lambdaQueryWrapper.like(SgmOtherVulnerability::getUrl,sgmOtherVulnerability.getUrl());
        }


        //根据漏洞类型查询
        if ( sgmOtherVulnerability.getRiskType()!=null){
            lambdaQueryWrapper.eq(SgmOtherVulnerability::getRiskType,sgmOtherVulnerability.getRiskType());
        }

        //根据漏洞状态查询
        if ( sgmOtherVulnerability.getStatus()!=null){
            lambdaQueryWrapper.eq(SgmOtherVulnerability::getStatus,sgmOtherVulnerability.getStatus());
        }



        //按照国家多选查询

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(SgmOtherVulnerability::getCountry,nationSelect);
        }


        //根据首次发现时间查询
        if(startDate!=null){
            lambdaQueryWrapper.ge(SgmOtherVulnerability::getCreateDate, startDate);
        }

        if(endDate!=null){
            lambdaQueryWrapper.le(SgmOtherVulnerability::getCreateDate, endDate);
        }

        lambdaQueryWrapper.orderByDesc(SgmOtherVulnerability::getCreateDate);//根据首次发现时间排序
        List<SgmOtherVulnerability> list = baseMapper.selectList(lambdaQueryWrapper);

        PageInfo<SgmOtherVulnerability> pageInfo = new PageInfo<SgmOtherVulnerability>(list);

        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        if(ids==null||ids.length==0){
            return 1;
        }
        LambdaUpdateWrapper<SgmOtherVulnerability> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(SgmOtherVulnerability::getStatus,updateStatus).in(SgmOtherVulnerability::getId , ids);
        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public Integer add(SgmOtherVulnerability sgmOtherVulnerability) {


        //判断接口有没有重复添加
        LambdaQueryWrapper<SgmOtherVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getUrl,sgmOtherVulnerability.getUrl());
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getRiskType,sgmOtherVulnerability.getRiskType());
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getCreateDate,sgmOtherVulnerability.getCreateDate());
        if (baseMapper.selectCount(lambdaQueryWrapper)!=0){
            return 0;
        }

        //首先需判断此接口之前有没有存在加白名单中
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getUrl,sgmOtherVulnerability.getUrl());
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getRiskType,sgmOtherVulnerability.getRiskType());
        lambdaQueryWrapper.eq(SgmOtherVulnerability::getStatus,4);//白名单状态

        if (baseMapper.selectCount(lambdaQueryWrapper)==0){
            //如果不存在于白名单，则插入数据
            sgmOtherVulnerability.setId(System.currentTimeMillis());
            sgmOtherVulnerability.setStatus(0);
            return baseMapper.insert(sgmOtherVulnerability);
        }

            return 0;

    }


}

